package com.ahu.controller;

import com.ahu.Repository.RepairCurRepository;
import com.ahu.Repository.UserInfoRepository;
import com.ahu.domain.RepairCur;
import com.ahu.domain.UserInfo;
import com.ahu.util.CreateOrderNumber;
import com.ahu.util.ExceptionManager;
import com.ahu.util.Response;
//import com.alibaba.fastjson.JSON;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created by qx on 2017/12/5.
 */
@RestController
@RequestMapping("/v1")
public class UploadController {
    private final RepairCurRepository repairCurRepository;
    private final UserInfoRepository userInfoRepository;
    private final ExceptionManager exceptionManager;

    @Autowired
    public UploadController(RepairCurRepository repairCurRepository, UserInfoRepository userInfoRepository, ExceptionManager exceptionManager) {
        this.repairCurRepository = repairCurRepository;
        this.userInfoRepository = userInfoRepository;
        this.exceptionManager = exceptionManager;
    }

    private static final Logger logger = LoggerFactory.getLogger(UploadController.class);

    @Transactional
    @ApiOperation(value = "图片及保修信息上传")
    @RequestMapping("/upload")
    public Response uploadPicture(HttpServletRequest request, HttpServletResponse response) throws Exception {
        MultipartHttpServletRequest params = ((MultipartHttpServletRequest) request);
//        List<MultipartFile> files = params.getFiles("upload");
//            MultipartFile file = ( params.getFiles("upload");
        MultipartFile file = params.getFile("upload");
//        MultipartFile file = null;
        BufferedOutputStream stream = null;
        String number = params.getParameter("number");
        String tel = params.getParameter("tel");
        String repairContext = params.getParameter("repairContext");
        String repairAddress = params.getParameter("repairAddress");
        String repairTime = params.getParameter("repairTime");
        String repairType = params.getParameter("repairType");
        String repairDate = params.getParameter("repairDate");
        String submitTime1 = params.getParameter("submitTime");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
        Date submitTime = sdf.parse(submitTime1);
        if (userInfoRepository.findByNumber(number) != null) {
            UserInfo userInfo = userInfoRepository.findByNumber(number);
            userInfo.setTel(tel);
//            for (int i = 0; i < files.size(); ++i) {
//                file = files.get(0);
            if (!file.isEmpty()) {
                try {
                    byte[] bytes = file.getBytes();
                    Date now = new Date();
                    SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
                    stream = new BufferedOutputStream(new FileOutputStream(
                            new File("C:" + File.separator + "repairimage" + File.separator + number + sdf1.format(now) + ".silk")));
                    stream.write(bytes);
                    stream.close();
                    RepairCur repairCur = new RepairCur();
                    repairCur.setRepairType(repairType);
                    repairCur.setNumber(number);
                    repairCur.setTel(tel);
                    repairCur.setRepairContext(repairContext);
                    repairCur.setRepairAddress(repairAddress);
                    repairCur.setRepairTime(repairTime);
                    repairCur.setRepairDate(repairDate);
                    repairCur.setCreateTime(new Date());
                    repairCur.setRepairStatus("待处理");
                    repairCur.setSubmitTime(submitTime);
                    repairCur.setUpdateTime(submitTime);
                    repairCur.setRepairImage(bytes);
                    repairCur.setRepairOrderNumber(CreateOrderNumber.createOrderNumber(now, userInfo.getId()));
                    repairCurRepository.save(repairCur);
                    return new Response().success(repairCur.getRepairImage());

                } catch (Exception e) {
                    e.printStackTrace();
                    stream = null;
                    throw exceptionManager.createException("2001");
                }
            }

//            }

        }
        throw exceptionManager.createException("1001");
    }


    @RequestMapping("/uploadVoice")
    public Response uploadVoice(HttpServletRequest request, HttpServletResponse response){
        MultipartHttpServletRequest params = (MultipartHttpServletRequest) request;
        MultipartFile file = params.getFile("upload");
        BufferedOutputStream stream = null;
        if (!file.isEmpty()) {
            try {
                byte[] bytes = file.getBytes();
                Date now = new Date();
                SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddHHmmss");
                stream = new BufferedOutputStream(new FileOutputStream(
                        new File("C:" + File.separator + "repairimage" + File.separator  + sdf1.format(now) + ".silk")));
                stream.write(bytes);
                System.out.println(bytes);
                stream.close();
                return new Response().success();

            } catch (FileNotFoundException e) {
                e.printStackTrace();
                throw exceptionManager.createException("2001");
            } catch (IOException e) {
                e.printStackTrace();
                throw exceptionManager.createException("2001");
            }

        }
        throw exceptionManager.createException("1001");
    }
}




